inline
int Thread::spawn_n (size_t thr_num, 
	THREAD_FUNC_T func, 
	void *arg/* = 0*/, 
	thread_t *thr_ids/* = 0*/, 
	int flags/* = THR_JOIN*/, 
	hthread_t *thr_handles/* = 0*/, 
	size_t stack_size/* = 0*/)
{
    size_t i = 0;
    for (; i < thr_num; ++i)
    {
	thread_t thr_id = 0;
	hthread_t thr_handle = 0;
	if (Thread::spawn (func, arg, &thr_id, flags, &thr_handle, stack_size) == 0)
	{
	    if (thr_ids != 0 && thr_id != NULL_thread)
		thr_ids[i] = thr_id;
	    if (thr_handles != 0 && thr_handle != 0)
		thr_handles[i] = thr_handle;
	}else
	    break;
    }
    return i;
}
inline
int Thread::join (hthread_t thr_handle)
{
#if defined NDK_LINUX
    int result = ::pthread_join (thr_handle, NULL);
    if (result != 0)
    {
	errno = result;
	return -1;
    }
    return 0;
#elif defined NDK_WIN32
    WaitForSingleObject (thr_id, INFINITE);
    ::CloseHandle (thr_id);
    return 0;
#endif
}
inline
int Thread::detach (thread_t thr_id)
{
#if defined NDK_LINUX
    int result = ::pthread_detach (thr_id);
    if (result != 0)
    {
	errno = result;
	return -1;
    }
    return 0;
#elif defined NDK_WIN32
    return 0;
#endif
}
inline
thread_t Thread::self ()
{
#if defined NDK_LINUX
    return ::pthread_self ();
#elif defined NDK_WIN32
    return ::GetCurrentThreadId ();
#endif
}
inline
int Thread::thr_equal (thread_t thr_id_1, thread_t thr_id_2)
{
#if defined NDK_LINUX
    return ::pthread_equal (thr_id_1, thr_id_2);
#elif defined NDK_WIN32
    return thr_id_1 == thr_id_2 ? 1 : 0;
#endif
}
inline
int Thread::kill (thread_t thr_id, int signum)
{
#if defined NDK_LINUX
    int result = ::pthread_kill (thr_id, signum);
    if (result != 0)
    {
	errno = result; 
	return -1;
    }
    return 0;
#elif defined NDK_WIN32
    unused_arg (thr_id);
    unused_arg (signum);
    return 0;
#endif
}
inline
void Thread::yield ()
{
#if defined NDK_LINUX
    ::sched_yield ();
#elif defined NDK_WIN32
    Sleep (0);
#endif
}
inline
int Thread::cancel (hthread_t thr_handle)
{
#if defined NDK_LINUX
    return ::pthread_cancel (thr_handle) == 0 ? 0 : -1;
#elif defined NDK_WIN32
    TerminateThread (thr_handle, 0);
    ::CloseHandle(thr_handle);
    return 0;
#endif
}
inline
void Thread::testcancel ()
{
#if defined NDK_LINUX
    ::pthread_testcancel ();
#elif defined NDK_WIN32
#endif
}


